<?php

class MShippingOut extends MY_Model
{
    const ORDER_ID_DESC = 1;
    const ORDER_ID_ASC = 2;

    const STATUS_DISABLED = 0;
    const STATUS_SUCCESS = 1;
    protected $table = 'shipping_out';

    function __construct()
    {
        parent::__construct();
    }

    public static function getOrderType($orderType = NULL)
    {
        $define = array(
            self::ORDER_ID_DESC => 'ID倒序',
            self::ORDER_ID_ASC => 'ID升序'
        );
        if ($orderType === NULL) {
            return $define;
        } else {
            return key_exists($orderType, $define) ? $define[$orderType] : '';
        }
    }

    public static function getState($state = NULL)
    {
        $define = array(self::STATUS_DISABLED => '<label class="label label-danger">作废</label>', self::STATUS_SUCCESS => '<label class="label label-success">成功</label>');
        if ($state === NULL) {
            return $define;
        } else {
            return key_exists($state, $define) ? $define[$state] : '';
        }
    }


    public function getList($limit, $sort = false)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->limit($limit['limit'], $limit['offset']);
        $this->formfilter->doFilter();

        if ($sort) {
            switch ($sort) {
                case self::ORDER_ID_DESC:
                    $this->db->order_by('id', 'desc');
                    break;
                case self::ORDER_ID_ASC:
                    $this->db->order_by('id', 'asc');
                    break;
            }
        } else {
            $this->db->order_by('id', 'desc');
        }

        $query = $this->db->get();
        return $query->result();
    }


    public function getCount()
    {
        $this->db->select('count(distinct id) as num');
        $this->db->from($this->table);
        $this->formfilter->doFilter();
        $tmp = $this->db->get()->result();
        return (int)$tmp[0]->num;
    }

    public function getById($id)
    {
        $query = $this->db->where('id', $id)->get($this->table);
        return $query->row();
    }

    public function getAllProducts(){
        $this->db->select( $this->table.'.*');
        $this->db->distinct();
        $this->db->from($this->table);
        $query = $this->db->get();
        $queryResult =  $query->result();
        $kvArr = array();
        foreach ($queryResult as $k=>$v){
            $kvArr[$v->id] = $v->product_name.'  (id => '.$v->id.')';
        }
        return $kvArr;
    }

    public function getOutAmountByDate($date,$shop_id,$city,$group_id){
        $this->db->select('*');
        $this->db->from($this->table);

        $this->db->where('status',1);
        if ($shop_id){
            $this->db->where('shop_id',$shop_id);
        }

        if ($group_id){
            $this->db->where('group_id', $group_id);
        }

        if ($city){
            $this->db->where('city',$city);
        }

        $query = $this->db->get();
        $list =  $query->result();
        $out_time = strtotime($date);
        $descAmount = 0;
        foreach ($list as $v){
            if ($v->out_date <= $out_time){
                $descAmount += $v->cost;
            }
        }
        return $descAmount;

    }
}
